package com.jkoss.wine.orders.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.orders.service.IOrderService;
import com.jkoss.wine.statistics.entity.Orders;


@Controller
@RequestMapping("/orders/orders")
public class OrderController extends BaseController {


	@Autowired
	private IOrderService iOrdersService;
	
	@RequestMapping("/list")
	@RequiresPermissions("/orders/orders/list")
	public String list(String orderNum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("orderNum", orderNum);
		Wrapper wrapper = Condition.create().like("order_num", orderNum); 
		
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			
		}
		Page resultPage = iOrdersService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "orders/orders/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/orders/orders/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
	
		return "orders/orders/edit";
	}

	@RequestMapping("/insert")	
	@RequiresPermissions("/orders/orders/toInsert")
	@ResponseBody
	public Object insert(@Valid Orders orders, HttpServletRequest request, HttpServletResponse response) {
		if (iOrdersService.insert(orders)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/orders/orders/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iOrdersService.selectById(id));
		return "orders/orders/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/orders/orders/toUpdate")
	@ResponseBody
	public Object update(@Valid Orders orders, HttpServletRequest request, HttpServletResponse response) {
		if (iOrdersService.updateById(orders)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/orders/orders/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iOrdersService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	

}

